package com.study.home;

import android.annotation.SuppressLint;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.common.base.activity.BaseThemeActivity;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.theme.ThemeHelper;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.bottomnavigation.BottomNavigationBar;
import com.common.ui.bottomnavigation.BottomNavigationItem;
import com.mikepenz.community_material_typeface_library.CommunityMaterial;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.study.R;
import com.study.locker.LockerFragment;
import com.study.statistical.StatisticalFragment;
import com.study.study.StudyFragment;
import com.study.user.UserFragment;
import butterknife.BindView;

public class HomeActivity extends BaseThemeActivity implements BottomNavigationBar.OnTabSelectedListener{

    @BindView(R.id.content_container)
    LinearLayout contentContainer;
    @BindView(R.id.bottomNavigationBar)
    BottomNavigationBar bottomNavigationBar;
    private BottomNavigationItem bottomNavigationItemOne, bottomNavigationItemTwo,
            bottomNavigationItemThree,bottomNavigationItemFour,bottomNavigationItemFive;
    private HomeFragment homeFragment;
    private StatisticalFragment statisticalFragment;
    private LockerFragment lockerFragment;
    private StudyFragment studyFragment;
    private UserFragment userFragment;
    private int index = 0;//0首页 1数据统计 2 锁机 3 学习 4用户
    private long exitTime = 0;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
                .navigationBarDarkIcon(true) //导航栏图标是深色，不写默认为亮色
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_home;
    }

    @SuppressLint("ResourceType")
    @Override
    public void updateUiElements() {
        super.updateUiElements();
        if(bottomNavigationBar!=null){
            bottomNavigationBar.clearAll();
            bottomNavigationItemOne = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_home)),
                    getString(R.string.home))
                    .setActiveColor(getThemeHelper().getPrimaryColor());
            bottomNavigationItemTwo = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (CommunityMaterial.Icon.cmd_database)),
                    getString(R.string.statistical))
                    .setActiveColor(getThemeHelper().getPrimaryColor());
            bottomNavigationItemThree = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_lock)),
                    getString(R.string.locker))
                    .setActiveColor(getThemeHelper().getPrimaryColor());
            bottomNavigationItemFour = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_book)),
                    getString(R.string.study))
                    .setActiveColor(getThemeHelper().getPrimaryColor());
            bottomNavigationItemFive = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (CommunityMaterial.Icon.cmd_account)),
                    getString(R.string.user))
                    .setActiveColor(getThemeHelper().getPrimaryColor());
            bottomNavigationBar
                    .addItem(bottomNavigationItemOne)
                    .addItem(bottomNavigationItemTwo)
                    .addItem(bottomNavigationItemThree)
                    .addItem(bottomNavigationItemFour)
                    .addItem(bottomNavigationItemFive)
                    .setFirstSelectedPosition(index)
                    .initialise();
            bottomNavigationBar.setTabSelectedListener(this);
        }
    }

    @Override
    public void init() {
        initViews();
    }

    @Override
    public void loadData() {

    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        //select file
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    @Override
    public void onTabSelected(int position) {
        index = position;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        hideFragments(transaction);
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.content_container, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                transaction.commit();
                break;
            case 1:
                if (statisticalFragment == null) {
                    statisticalFragment = new StatisticalFragment();
                    transaction.add(R.id.content_container, statisticalFragment);
                } else {
                    transaction.show(statisticalFragment);
                }
                transaction.commit();
                break;
            case 2:
                if (lockerFragment == null) {
                    lockerFragment = new LockerFragment();
                    transaction.add(R.id.content_container, lockerFragment);
                } else {
                    transaction.show(lockerFragment);
                }
                transaction.commit();
                break;
            case 3:
                if (studyFragment == null) {
                    studyFragment = new StudyFragment();
                    transaction.add(R.id.content_container, studyFragment);
                } else {
                    transaction.show(studyFragment);
                }
                transaction.commit();
                break;
            case 4:
                if (userFragment == null) {
                    userFragment = new UserFragment();
                    transaction.add(R.id.content_container, userFragment);
                } else {
                    transaction.show(userFragment);
                }
                transaction.commit();
                break;
            default:
                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    private void initViews(){
        /* InputMethodManager inputMethodManager =
                (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);*/
        /**
         * BadgeItem 红点
         */
       /* BadgeItem badgeItem=new BadgeItem().setBorderWidth(1).setBackgroundColorResource(R.color.colorAccent)
                .setText("2").setHideOnSelect(true);*/
        //bottomNavigationBar.setFab(floatingActionButton);

        //如果Item的个数<=3就会使用MODE_FIXED模式，否则使用MODE_SHIFTING模式
        //bottomNavigationBar.setMode(BottomNavigationBar.MODE_DEFAULT);
        //填充模式，未选中的Item会显示文字，没有换挡动画。
        //bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        //换挡模式，未选中的Item不会显示文字，选中的会显示文字。在切换的时候会有一个像换挡的动画
        //bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);

        //如果设置的Mode为MODE_FIXED，将使用BACKGROUND_STYLE_STATIC 。如果Mode为MODE_SHIFTING将使用BACKGROUND_STYLE_RIPPLE。
        //bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        //点击的时候没有水波纹效果
        //bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        //点击的时候有水波纹效果
        //bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        bottomNavigationBar.clearAll();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);

       /* bottomNavigationItemOne = new BottomNavigationItem(R.mipmap.user, getString(R.string.home))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemTwo = new BottomNavigationItem(R.mipmap.user, getString(R.string.video))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemThree = new BottomNavigationItem(R.mipmap.user, getString(R.string.voice))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemFour = new BottomNavigationItem(R.mipmap.user, getString(R.string.user))
                .setActiveColorResource(R.color.black);*/
        bottomNavigationItemOne = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_home)),
                getString(R.string.home))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemTwo = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (CommunityMaterial.Icon.cmd_database)),
                getString(R.string.statistical))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemThree = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_lock)),
                getString(R.string.locker))
                .setActiveColorResource(R.color.black);

        bottomNavigationItemFour = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_book)),
                getString(R.string.study))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemFive = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (CommunityMaterial.Icon.cmd_account)),
                getString(R.string.user))
                .setActiveColor(getThemeHelper().getPrimaryColor());

        bottomNavigationBar
                .addItem(bottomNavigationItemOne)
                .addItem(bottomNavigationItemTwo)
                .addItem(bottomNavigationItemThree)
                .addItem(bottomNavigationItemFour)
                .addItem(bottomNavigationItemFive)
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
        //AppDroid.getInstance().resumePush();
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
        }
        transaction.replace(R.id.content_container, homeFragment);
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {

        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (statisticalFragment != null) {
            transaction.hide(statisticalFragment);
        }
        if (lockerFragment != null) {
            transaction.hide(lockerFragment);
        }
        if (studyFragment != null) {
            transaction.hide(studyFragment);
        }
        if (userFragment != null) {
            transaction.hide(userFragment);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), getString(R.string.click_again_exit_app), Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
